import { pathToBase64 } from 'image-tools'

// 格式时间
export const secondsToTime = (secs) => {
  let hours = Math.floor(secs / (60 * 60))
  let divisor_for_minutes = secs % (60 * 60)
  let minutes = Math.floor(divisor_for_minutes / 60)
  let divisor_for_seconds = divisor_for_minutes % 60
  let seconds = Math.floor(divisor_for_seconds)
  let obj = {
    h: hours,
    m: minutes,
    s: seconds,
  }
  return obj
}

// 将数组的前几位移动出，并且返回新数组
export const moveElementsToNewArray = (array, number) => {
	var newArray = array.splice(0, number);
	let reArr = []
	for(let i = 0; i < newArray.length; i++) {
		reArr.push({
			...newArray[i],
			id: i+1,
			userid:i+1,
		})
	}
	// array.splice(0, number);
	return reArr;
}

// 获取顶部安全距离
export const getClientHeight = () => {
	const getS = uni.getSystemInfoSync()
	const system = getS.platform;
	if(getS.platform === 'ios') {
		return 44+getS.statusBarHeight;
	} else if(getS.platform === 'android') {
		return 48+getS.statusBarHeight;
	} else {
		return 0;
	}
}

// 将数字转1万、1亿...
export const convertToKMB = (number) => {
  const units = ['', '万', '亿', '十亿', '百亿', '千亿'];
  let index = 0;
  while (number >= 10000) {
    number = number / 10000;
    index++;
  }
  return number.toFixed() + units[index];
}

// 图片转base64
export const imgTurnBase64 = (path) => {
	function isBase64Image(str) {
		const base64Regex = /^data:image\/([a-z]+);base64,/;
		return base64Regex.test(str);
	}
	return new Promise((resolve, reject) => {
		const isBase64 = isBase64Image(path)
		if(isBase64) {
			resolve(path)
			return
		}
		pathToBase64(path)
			.then(base64 => {
				resolve(base64)
			})
			.catch(error => {
				reject(error)
			})
	})
}
